package com.example.springbootmybatis.util;

import com.example.springbootmybatis.entity.ResultMap;
import com.example.springbootmybatis.service.ECharsDataService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 作者： Administrator
 * 日期: 2023/12/14/014
 * Description :
 */
public class ToolUtil {


    public static ResultMap<? extends Object> getResult(Object obj, String option) {
        ResultMap<Object> resultMap = new ResultMap<>();
        if (obj != null) {
            resultMap.setCode(200);
            resultMap.setResult(obj);
            resultMap.setMsg(option + "成功");
        } else {
            resultMap.setCode(-1);
            resultMap.setMsg(option + "失败");
        }
        return resultMap;

    }

    /**
     * service实现类
     */
    public static PageInfo getResultForPage(List<?> list) {
        // 设置分页
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    public static String DateFormat(Date date) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        return sf.format(date);
    }

    private static Date parseDate(String datestr) throws ParseException {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        return sf.parse(datestr);
    }

    /**
     * 检查是不是当日
     * @param theDate
     * @return
     * @throws ParseException
     */
    private static boolean checkIsToday(Date theDate) throws ParseException {
        Date now = new Date();
        Date date = parseDate(DateFormat(now));
        int i = theDate.compareTo(date);
        return i == 0 ? true : false;
    }

    /**
     * 如果不是当日就删除缓存中的过期时间的数据
     * @param redisTemplate
     * @throws ParseException
     */
    public static void  DelRedisDate(RedisTemplate redisTemplate) throws ParseException {
        Set keys = redisTemplate.keys("*");
        Iterator iterator = keys.iterator();
        while (iterator.hasNext()) {
            String key = (String) iterator.next();
            String keyDate = key.substring(0, key.lastIndexOf("_"));
            Date date = parseDate(keyDate);
            boolean b = checkIsToday(date);

            if (!b) {
                redisTemplate.delete(key);
            }

        }
    }

    /**
     * 添加访问量
     * @param redisTemplate
     * @throws ParseException
     */
    public static void setRedisDataVisit(RedisTemplate redisTemplate) throws ParseException {
      //  DelRedisDate(redisTemplate);
       // int visitorCount= redisTemplate.opsForValue().get(ToolUtil.DateFormat(new Date()) + "_visitor")==null?0:(int)redisTemplate.opsForValue().get(ToolUtil.DateFormat(new Date()) + "_visitor");
        redisTemplate.opsForValue().increment(ToolUtil.DateFormat(new Date()) + "_visitor");

    }
    /**
     * 添加访问量
     * @param redisTemplate
     * @throws ParseException
     */

    public static void setRedisDataBorrow(RedisTemplate redisTemplate) throws ParseException {
      //  DelRedisDate(redisTemplate);
      //  int borrowCount= redisTemplate.opsForValue().get(ToolUtil.DateFormat(new Date()) + "_Borrow")==null?0:(int)redisTemplate.opsForValue().get(ToolUtil.DateFormat(new Date()) + "_Borrow");
        redisTemplate.opsForValue().increment(ToolUtil.DateFormat(new Date()) + "_Borrow");


    }
    /**
     * 添加访问量
     * @param redisTemplate
     * @throws ParseException
     */
    public static void setRedisDataLend(RedisTemplate redisTemplate) throws ParseException {
      //  DelRedisDate(redisTemplate);
        redisTemplate.opsForValue().increment(ToolUtil.DateFormat(new Date()) + "_lend");
    }
}

